//承运商管理列表

/**
 * 添加承运商视图
 */
interface ICarrierManagenAddViewModel extends ng.IScope {
    /**
     * 承运商类型下拉
     */
    carrierTypeDropDown: ValueListItemResponse[];
    /**
     * 承运商等级下拉
     */
    carrierLevelDropDown: ValueListItemResponse[];
    /**
     * 承运商类型
     */
    carrierType: string;
    /**
     * 承运商名称
     */
    carrierName: string;
    /**
     * 承运商号码
     */
    carrierPhone: string;
    /**
     * 承运商地址
     */
    carrierAddress?: string;
    /**
     * 承运商等级
     */
    carrierLevel: string;
    /**
     * 备注
     */
    carrierRemarks?: string;
    /**
     * 保存
     */
    carrierSave: () => void;

    /**
     * 返回事件
     */
    backUrl:()=>void;
}

class CarrierManagentAddController {
    gridApi: uiGrid.IGridApiOf<CarrierListItemResponse>;

    constructor(private $scope: ICarrierManagenAddViewModel, private carrierService: ICarrierService,
        private valueService: IValueService, private $state: angular.ui.IStateService, private $ngBootbox: BootboxService) {
        this.init();
    }

    init(): void {
        this.clearInput();
        this.loadCarrierTypeDropDown();
        this.loadCarrierLevelDropDown();

        this.$scope.carrierSave = this.addCarrier;

        this.$scope.backUrl = this.backUrl
    }

    /**
     * 下拉承运商类别事件
     */
    loadCarrierTypeDropDown(): void {
        this.$scope.carrierTypeDropDown = this.valueService.getCarrierTypeList().data
    }

    /**
     * 下拉承运商级别事件
     */
    loadCarrierLevelDropDown(): void {
        this.$scope.carrierLevelDropDown = this.valueService.getCarrierLevelList().data
    }

    /**
     * 初始化清空表单
     */
    clearInput=()=> {
        this.$scope.carrierType = "";
        this.$scope.carrierName = "";
        this.$scope.carrierPhone = "";
        this.$scope.carrierAddress = "";
        this.$scope.carrierRemarks = "";
        this.$scope.carrierLevel = "";
    }

    /**
     *添加保存
     */
    addCarrier = () => {
        if (!$("#form").valid()) { return; }
        this.$ngBootbox.confirm("确定保存数据吗？").then(result => {
            this.carrierService.add(
                this.$scope.carrierName,
                this.$scope.carrierType,
                this.$scope.carrierPhone,
                this.$scope.carrierLevel,
                this.$scope.carrierAddress,
                this.$scope.carrierRemarks
            ).then(result => {
                this.clearInput();
                let carrier = {
                    carrierId:result.data.extData,
                    carrierName: this.$scope.carrierName
                }
                window.localStorage.setItem("carrier",JSON.stringify(carrier));
                this.$state.go("app.carrier.carrierManagentEdit", { carrierId: result.data.extData });
            });
        });
    }

    backUrl=()=>{
        this.$state.go('app.carrier.carrierManagent',{ name:'carrierManagentAdd' });
    }
}

angular.module("tms").controller("carrierManagentAddController", ["$scope", "carrierService", "valueService", "$state", "$ngBootbox", CarrierManagentAddController]);